package com.appcenter.jz.app;

import com.appcenter.jz.expandmodel.UserDetaileInfo;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import java.security.Principal;
import java.util.ArrayList;
import java.util.HashMap;


public class CurrentUser {
    private static String userId="jim";
    private static String userCode="jim";
    private static String userName="jim";

    private static String token="";

    private static Object orgs;
    private static String roles="";
    private static Object roleLists;



    public static String getToken() {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();

        if (principal instanceof UserDetaileInfo) {
            return ((UserDetaileInfo) principal).getToken();
        }
        return String.valueOf(principal);
    }

    public static void setToken(String token) {
        CurrentUser.token = token;
    }

    public static String getUserId() {

        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();

        if (principal instanceof UserDetaileInfo) {
            return ((UserDetaileInfo) principal).getUserId();
        }
        return String.valueOf(principal);
    }

    public static void setUserId(String userId) {
        CurrentUser.userId = userId;
    }

    public static String getUserCode() {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();

        if (principal instanceof UserDetaileInfo) {

            return ((UserDetaileInfo) principal).getUserCode();

        }

        return String.valueOf(principal);
    }

    public static void setUserCode(String userCode) {
        CurrentUser.userCode = userCode;
    }

    public static String getUserName() {

		Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();

		if (principal instanceof UserDetaileInfo) {

			return ((UserDetaileInfo) principal).getUsername();

		}

		if (principal instanceof Principal) {

			return ((Principal) principal).getName();

		}

		return String.valueOf(principal);

    	//return userName;
    }

    public static void setUserName(String userName) {
        CurrentUser.userName = userName;
    }


    public static Object getOrgs() {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();

        if (principal instanceof UserDetaileInfo) {
            return ((UserDetaileInfo) principal).getOrgs();
        }
        return principal;
    }

    public static void setOrgs(Object orgs) {
        CurrentUser.orgs = orgs;
    }

    public static String getRoles() {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();

        if (principal instanceof UserDetaileInfo) {
            return ((UserDetaileInfo) principal).getRoles();
        }
        return String.valueOf(principal);
    }

    public static void setRoles(String roles) {
        CurrentUser.roles = roles;
    }

    public static Object getRoleLists() {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();

        if (principal instanceof UserDetaileInfo) {
            return ((UserDetaileInfo) principal).getRoleLists();
        }
        return principal;
    }

    public static void setRoleLists(Object roleLists) {
        CurrentUser.roleLists = roleLists;
    }
}
